import { queryRecommend, removeRecommend, addRecommend } from '../services/api';

export default {
  namespace: 'recommend',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryRecommend, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addRecommend, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response); else return response;
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeRecommend, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      if (!action.payload) {
        return {...state};
      } else {
        return {
          ...state,
          data: action.payload,
        };
      }
    },
  },
};
